package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.consignor.CustomerCarBrand;
import com.example.chcrm.entity.consignor.CustomerCarModel;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.service.CustomerCarBrandService;
import com.example.chcrm.service.CustomerCarModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 邹
 * @Date 2025/4/20 17:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/customerCarModel")
@CrossOrigin // 允许跨域访问
public class CustomerCarModelController {
    @Autowired
    private CustomerCarModelService customerCarModelService;

    /**
     * 分页查询
     */
    @PostMapping("/pageList")
    public ResponseMsg<IPage<CustomerCarModel>> carModelList(@RequestBody CustomerCarModel customerCarModel) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return customerCarModelService.carModelList(customerCarModel, empVo);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public ResponseMsg<Object> addCarModel(@RequestBody CustomerCarModel customerCarModel) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        //获取登录用户信息
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return customerCarModelService.addCarModel(customerCarModel, empVo);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> carModelDetail(@PathVariable String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (id == null || id.equals("")) {
            msg.fail("请选择要查询的数据！");
            return msg;
        }
        return customerCarModelService.carModelDetail(id);
    }

    /**
     * 修改
     *
     * @param customerCarModel
     * @return
     */
    @PostMapping("/update")
    public ResponseMsg<Object> updateCarModel(@RequestBody CustomerCarModel customerCarModel) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        //校验传递信息
        if (customerCarModel.getId() == null || customerCarModel.getId() == "") {
            msg.fail("请选择要修改的数据");
            return msg;
        }
        //获取登录用户信息
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return customerCarModelService.updateCarModel(customerCarModel, empVo);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public ResponseMsg<Object> deleteCarmodel(CustomerCarModel customerCarModel) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        //获取登录用户信息
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return customerCarModelService.deleteCarmodel(customerCarModel, empVo);

    }


}
